package com.ihr360.rest.controller;

import com.ihr360.commons.request.Ihr360SearchBody;
import com.ihr360.rest.service.SimpleRestService;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @author david.wei
 * @param <T>
 */
@RestController
public  interface RestSimpleWithStringIdController<T> {

    /**
     * 获取SimpleRestService实现
     *
     * @return
     */
    SimpleRestService<T, String> getSimpleRestService();

    @DeleteMapping(value = "/",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    default Boolean delete(@RequestBody String[] ids) {
        getSimpleRestService().batchDelete(Arrays.asList(ids));
        return true;
    }

    @GetMapping(value = "/{id}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    default T get(@PathVariable(name = "id") String id) {
        return getSimpleRestService().getOne(id);
    }

    @PostMapping(value = "/search",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    default Page<T> executeSearch(@RequestBody Ihr360SearchBody<T> searchBody) {
        return getSimpleRestService().search(searchBody.getSpecification(), searchBody.getPageable(), false);
    }
}